package com.mapper;

import com.pojo.Teacher;
import com.pojo.Topic;
import com.pojo.TopicType;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface TopicMapper {

    @Select("select * from topic where type_id = #{type_id} and state = 2 and createTime = #{year} limit #{page},#{pagesize}")
    List<Topic> getByTypePage(Integer type_id,String year,Integer page,Integer pagesize);

    @Select("select count(*) from topic where type_id = #{type_id} and state = 2 and createTime = #{year}")
    Integer countTopic(Integer type_id,String year);

    @Select("select * from teacher where id = #{id}")
    Teacher getTeacherInfo(Integer id);

    @Select("select * from topic where id = #{id}")
    Topic getById(Integer id);

    @Select("select * from topic where state = 0 limit #{page},#{pagesize}")
    List<Topic> getInspect(Integer page,Integer pagesize);

    @Select("select count(*) from topic where state = 0")
    Integer countInspect();

    @Select("select * from topic where teacher_id = #{teacher_id} order by state,createTime desc limit #{page},#{pagesize}")
    List<Topic> getTeacherAllTopic(Integer teacher_id,Integer page,Integer pagesize);

    @Select("select * from topic where teacher_id = #{teacher_id} and state = 2 limit #{page},#{pagesize}")
    List<Topic> getTeacherApprovedTopic(Integer teacher_id,Integer page,Integer pagesize);

    @Select("select * from topic_type")
    List<TopicType> getTypes();

    @Insert("insert into topic_type(name) value(#{name})")
    void addType(String name);

    @Select("select * from topic_type where id = #{id}")
    TopicType getTypeById(Integer id);

    @Update("update topic_type set name = #{name} where id = #{id}")
    void updateType(Integer id,String name);

    @Delete("delete from topic_type where id = #{id}")
    void delType(Integer id);

    @Update("update topic set state = #{state} where id = #{topic_id}")
    void examine(Integer topic_id,Integer state);

    @Insert("insert into topic(type_id,teacher_id,members,title,conents,img,createTime) value(#{type_id},#{teacher_id},#{members},#{title},#{conents},#{img},YEAR(NOW()))")
    void addTopic(Integer type_id,Integer teacher_id,Integer members,String title,String conents,String img);

    @Update("update topic set type_id = #{type_id},teacher_id = #{teacher_id},members = #{members},title = #{title},conents = #{conents},img = #{img} where id = #{id}")
    void updateTopic(Integer id,Integer type_id,Integer teacher_id,Integer members,String title,String conents,String img);

    @Delete("delete from topic where id = #{id}")
    void delTopic(Integer id);

    @Update("update team set topic_id = #{topic_id},topic_state = 0 where id = #{team_id}")
    void subscribe(Integer team_id,Integer topic_id);

    @Select("select * from topic where title like #{like}")
    List<Topic> search(String like);

    @Select("select distinct createTime from topic order by createTime desc")
    List<String> getYears();

    @Select("select name from topic_type where id = #{id}")
    String getTypeNameById(Integer id);

    @Select("select count(*) from topic where teacher_id = #{teacher_id}")
    Integer countAll(Integer teacher_id);

    @Select("select count(*) from topic where teacher_id = #{teacher_id} and state = 2")
    Integer countByState(Integer teacher_id);

}
